package com.ithuang.quartz.utils;

import com.ithuang.quartz.domain.SysJob;
import org.quartz.JobExecutionContext;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.ithuang.quartz.constant.ScheduleConstants.TASK_PARAMS;

/**
 * 执行定时任务的方法
 * @author HTT
 */
public class JobExecuteUtils {

    /**
     * 获取bean并执行对应的方法
     * @param jobExecutionContext
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static void executeMethod(JobExecutionContext jobExecutionContext) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Object param = jobExecutionContext.getMergedJobDataMap().get(TASK_PARAMS);
        SysJob sysJob = new SysJob();
        BeanUtils.copyProperties(param,sysJob);
        Object bean = SpringUtils.getBean(sysJob.getBeanTarget());
        Method method = bean.getClass().getMethod(sysJob.getBeanMethodTarget());
        method.invoke(bean);
    }
}
